package org.lsc.utils;

import com.alibaba.fastjson.JSON;
import netscape.javascript.JSObject;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by dell on 2017-08-24.
 */
public class ReadFileFromDirectory {
    public static List getFiles(String path){
        File root = new File(path);
        List<File> files = new ArrayList<>();
        if(!root.isDirectory()){
            files.add(root);
        }else{
            File[] subFiles = root.listFiles();
            for(File f : subFiles){
                files.addAll(getFiles(f.getAbsolutePath()));
            }
        }
        return files;
    }

    public static void main(String[] args) throws IOException {


        File file = new File("C:\\Users\\M8600t\\Desktop\\数据转移代码\\ed96755edf0e8615d2079537dbf947ff_t.jpg");
        BASE64Decoder decoder=new BASE64Decoder();
        byte[] imgbyte = decoder.decodeBuffer(String.valueOf(file));//解码Base64图片数据
        System.out.println(Arrays.toString(imgbyte));
        System.out.println(file.getAbsoluteFile());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getCanonicalPath());
        System.out.println(file.getParentFile());
        System.out.println(file.getPath());
        System.out.println(file.getName());


       /* //方式1 推荐
        new ReadFileFromDirectory().copyFolder("C:\\Users\\M8600t\\Desktop\\images","C:\\Users\\M8600t\\Desktop\\newImages");
        //方式2
        List<File> files = getFiles("C:\\Users\\M8600t\\Desktop\\images");
        File newFiles = new File("C:\\Users\\M8600t\\Desktop\\newImages");
        newFiles.mkdirs();
        for(File file : files){
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\M8600t\\Desktop\\newImages\\" +  file.getName() ));
            byte[] read = new byte[1024];
            int len = 0;
            while((len = fis.read(read))!= -1){
                fos.write(read,0,len);
            }
            fis.close();
            fos.flush();
            fos.close();

            file.delete();
            System.out.println(file.getName());
        }
        System.out.println(files.size());
*/


    }



    public void copyFile(String strOldpath,String strNewPath)
    {
        try
        {
            File fOldFile = new File(strOldpath);
            if (fOldFile.exists())
            {
                int bytesum = 0;
                int byteread = 0;
                InputStream inputStream = new FileInputStream(fOldFile);
                FileOutputStream fileOutputStream = new FileOutputStream(strNewPath);
                byte[] buffer = new byte[1444];
                while ( (byteread = inputStream.read(buffer)) != -1)
                {
                    bytesum += byteread; //这一行是记录文件大小的，可以删去
                    fileOutputStream.write(buffer, 0, byteread);//三个参数，第一个参数是写的内容，
                    //第二个参数是从什么地方开始写，第三个参数是需要写的大小
                }
                inputStream.close();
                fileOutputStream.close();
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("复制单个文件出错");
            e.printStackTrace();
        }
    }

    public void copyFolder(String strPatientImageOldPath,String strPatientImageNewPath)
    {
        File fOldFolder = new File(strPatientImageOldPath);//旧文件夹
        try
        {
            File fNewFolder = new File(strPatientImageNewPath);//新文件夹
            if (!fNewFolder.exists())
            {
                fNewFolder.mkdirs();//不存在就创建一个文件夹
            }
            File [] arrFiles = fOldFolder.listFiles();//获取旧文件夹里面所有的文件
            for (int i = 0; i < arrFiles.length; i++)
            {
                //从原来的路径拷贝到现在的路径，拷贝一个文件
                if (!arrFiles[i].isDirectory())
                {
                    //写OSS 上传代码即可
                    copyFile(strPatientImageOldPath+"/"+arrFiles[i].getName(), strPatientImageNewPath+"/"+arrFiles[i].getName());
                }else {
                    copyFolder(strPatientImageOldPath+"/"+arrFiles[i].getName(),strPatientImageNewPath+"/"+arrFiles[i].getName());
                }
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

}
